#!/usr/bin/env bash

if ! rpm -qa|grep -q ntpdate; then
  yum install -y ntpdate
fi
ntpdate time.windows.com

yum install -y libtermcap-devel ncurses-devel libevent-devel readline-devel
yum install -y gcc gcc-c++ autoconf automake
yum install -y zlib zlib-devel openssl openssl-devel pcre-devel

LUA_NGINX_MODULE_URL="https://github.com/openresty/lua-nginx-module/archive/v0.10.15.tar.gz"
LUA_URL="http://www.lua.org/ftp/lua-5.3.5.tar.gz"
NGX_DEVEL_KIT_URL="https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.tar.gz"
NGINX_URL="http://nginx.org/download/nginx-1.17.1.tar.gz"
LUA_JIT_URL="https://github.com/openresty/luajit2/archive/v2.1-20190626.tar.gz"

curl -#SL -o- ${LUA_URL}|tar -zxvf - --transform=s+^[^/]*+lua+
curl -#SL -o- ${LUA_NGINX_MODULE_URL}|tar -zxvf - --transform=s+^[^/]*+lua_nginx_module+
curl -#SL -o- ${NGX_DEVEL_KIT_URL}|tar -zxvf - --transform=s+^[^/]*+ngx_devel_kit+
curl -#SL -o- ${NGINX_URL}|tar -zxvf - --transform=s+^[^/]*+nginx+
curl -#SL -o- ${LUA_JIT_URL}|tar -zxvf - --transform=s+^[^/]*+lua_jit+

CUR_DIR="$(pwd)"
cd "$CUR_DIR/lua"
make linux test
make install

cd "$CUR_DIR/lua_jit"
make
make install

export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.1

cd "$CUR_DIR/nginx"

./configure \
  --prefix=/usr \
  --sbin-path=/usr/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --user=nginx \
  --group=nginx \
  --add-module=../ngx_devel_kit \
  --add-module=../lua_nginx_module

make
make install
